package com.pic.mgt.syushin;

import com.pic.mgt.executer.UnitDispatchException;
import com.pic.mgt.executer.UnitDispatcher;
import com.pic.mgt.executer.UnitExecuter;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.syushin.unit.BatchSyushinBusinessUnit;
import com.pic.mgt.syushin.unit.RealSyushinBusinessUnit;
import com.pic.mgt.unit.Unit;

public final class SyushinBusinessUnitDispatcher extends UnitDispatcher {
	private UnitExecuter realSyushinBusinessUnitExecuter;
	private UnitExecuter batchSyushinBusinessUnitExecuter;

	@Override
	protected void execute(Unit syushinBusinessUnit) {
		// 即時集信
		if (RealSyushinBusinessUnit.class.isInstance(syushinBusinessUnit)) {
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this, "Dispatching unit '{}' to real syushin business unit executer.",
						syushinBusinessUnit.getUnitName());
			}
			// ********************** Log **********************
			
			this.realSyushinBusinessUnitExecuter.execute(syushinBusinessUnit);
		}
		// 批次集信
		else if (BatchSyushinBusinessUnit.class.isInstance(syushinBusinessUnit)) {
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this, "Dispatching unit '{}' to batch syushin business unit executer.",
						syushinBusinessUnit.getUnitName());
			}
			// ********************** Log **********************
			
			this.batchSyushinBusinessUnitExecuter.execute(syushinBusinessUnit);
		}
		else {
			try {
				throw new UnitDispatchException(
						"SUDA-308 Dispatcher can't find match executer for unit '" +
						syushinBusinessUnit.getClass().getName() + "'.");
			} catch (UnitDispatchException e) {
				// ********************** Log **********************
				IgxLog.messageLog.error(this, e.getMessage());
				// ********************** Log **********************
			}
		}
	}
	
	// --- getter and setter ---
	
	public void setRealSyushinBusinessUnitExecuter(UnitExecuter realSyushinBusinessUnitExecuter) {
		this.realSyushinBusinessUnitExecuter = realSyushinBusinessUnitExecuter;
	}

	public void setBatchSyushinBusinessUnitExecuter(
			UnitExecuter batchSyushinBusinessUnitExecuter) {
		this.batchSyushinBusinessUnitExecuter = batchSyushinBusinessUnitExecuter;
	}
	
}
